package com.lindenvalley.ch.ice;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainPreviewActivity extends Activity{
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView cameraView = (ImageView) findViewById(R.id.imageCameraView);
        ImageView getPicture = (ImageView) findViewById(R.id.imageTakePhoto);
        MyTouchListener listener = new MyTouchListener(); 
        
        cameraView.setOnTouchListener(listener);
        getPicture.setOnTouchListener(listener);
    }
	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	  if (resultCode == Activity.RESULT_OK && requestCode == 0) {
		  Intent intent = new Intent(this, IceApplicationActivity.class);
		  Uri dataUri = data.getData();
		  if (dataUri != null){			  
			  intent.setData(dataUri);
			  startActivity(intent);
		  }
	  }
	}
	
	@Override
	protected Dialog onCreateDialog (int id){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		LayoutInflater inflater = LayoutInflater.from(this);
		View dialogView = inflater.inflate(R.layout.about, null);
		TextView tv = (TextView) dialogView.findViewById(R.id.aboutTextView1);
		tv.setMovementMethod(LinkMovementMethod.getInstance());
		builder.setView(dialogView);
		builder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				
			}
		});
		return builder.create();
		
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	
    	int itemId = item.getItemId();
		switch (itemId) {
		case R.id.itemAbout:						
			showDialog(1);
			break;

		}
		return true;
    }
	
	class MyTouchListener implements View.OnTouchListener{

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN){
				if (v.getId() == R.id.imageCameraView){
					//TODO intent for start preview camera
					Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
					startActivityForResult(intent, 0);
					
				} else if (v.getId() == R.id.imageTakePhoto){
					Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
					intent.setType("image/*"); 
					startActivityForResult(intent, 0);

				}
				
				//TODO intent for show dialog send photo to someone
//				Intent intent = new Intent("android.intent.action.SEND");
//				intent.addCategory("android.intent.category.DEFAULT");
//				intent.setType("image/*");
				

			}
			return true;
		}
		
	}


}
